package com.sgb.member.weight;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.R;
import com.sgb.member.ui.adapter.MemberTypeAdapter;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择会员类型dialog
 */
public class MemberTypeDialog extends DialogFragment {
    private Context mContext;
    private List<CommonPopEntity> list_1;
    private List<CommonPopEntity> list_2 = new ArrayList<>();
    private List<CommonPopEntity> list_3 = new ArrayList<>();
    private MemberTypeAdapter adapter1, adapter2, adapter3;
    private int p1, p2, p3 = 0;
    private DialogCallback callback;

    private String firstCategory, secondCategory, thirdCategory = "";

    public void setDialogDialogCallback(DialogCallback callback) {
        this.callback = callback;
    }

    public MemberTypeDialog(Context context, List<CommonPopEntity> alls, String firstCategory, String secondCategory, String thirdCategory) {
        this.mContext = context;
        this.list_1 = alls;
        this.firstCategory = firstCategory;
        this.secondCategory = secondCategory;
        this.thirdCategory = thirdCategory;

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_frag_member_type, null);
        FrameLayout fl_1 = view.findViewById(R.id.fl_1);
        ImageView arrow_layout_1 = view.findViewById(R.id.arrow_layout_1);
        ImageView arrow_layout_up_1 = view.findViewById(R.id.arrow_layout_up_1);
        RecyclerView rv_1 = view.findViewById(R.id.rv_1);
        View l_1 = view.findViewById(R.id.l_1);

        FrameLayout fl_2 = view.findViewById(R.id.fl_2);
        ImageView arrow_layout_2 = view.findViewById(R.id.arrow_layout_2);
        ImageView arrow_layout_up_2 = view.findViewById(R.id.arrow_layout_up_2);
        RecyclerView rv_2 = view.findViewById(R.id.rv_2);
        View l_2 = view.findViewById(R.id.l_2);

        FrameLayout fl_3 = view.findViewById(R.id.fl_3);
        ImageView arrow_layout_3 = view.findViewById(R.id.arrow_layout_3);
        ImageView arrow_layout_up_3 = view.findViewById(R.id.arrow_layout_up_3);
        RecyclerView rv_3 = view.findViewById(R.id.rv_3);
        if (list_1.size() > 11) {
            arrow_layout_1.setVisibility(View.VISIBLE);
        } else {
            arrow_layout_1.setVisibility(View.GONE);
        }

        //显示全部数据
        arrow_layout_2.setOnClickListener(v -> {
            if (list_1.get(p1).getChildren().size() > 11) {
                arrow_layout_2.setVisibility(View.GONE);
                arrow_layout_up_2.setVisibility(View.VISIBLE);
                list_2.clear();
                list_2.add(new CommonPopEntity("", "全部"));
                list_2.addAll(list_1.get(p1).getChildren());
                adapter2.notifyDataSetChanged();
            }
        });
        //显示11条数据
        arrow_layout_up_2.setOnClickListener(v -> {
            if (list_1.get(p1).getChildren().size() > 11) {
                arrow_layout_up_2.setVisibility(View.GONE);
                arrow_layout_2.setVisibility(View.VISIBLE);
                list_2.clear();
                list_2.add(new CommonPopEntity("", "全部"));
                list_2.addAll(list_1.get(p1).getChildren().subList(0, 11));
                adapter2.notifyDataSetChanged();
            }
        });

        arrow_layout_3.setOnClickListener(v -> {
            if (list_2.get(p2).getChildren().size() > 11) {
                arrow_layout_3.setVisibility(View.GONE);
                arrow_layout_up_3.setVisibility(View.VISIBLE);
                list_3.clear();
                list_3.add(new CommonPopEntity("", "全部"));
                list_3.addAll(list_2.get(p2).getChildren());
                adapter3.notifyDataSetChanged();
            }
        });
        arrow_layout_up_3.setOnClickListener(v -> {
            if (list_2.get(p2).getChildren().size() > 11) {
                arrow_layout_up_3.setVisibility(View.GONE);
                arrow_layout_3.setVisibility(View.VISIBLE);
                list_3.clear();
                list_3.add(new CommonPopEntity("", "全部"));
                list_3.addAll(list_2.get(p2).getChildren().subList(0, 11));
                adapter3.notifyDataSetChanged();
            }
        });

        rv_1.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter1 = new MemberTypeAdapter(mContext, list_1);
        adapter1.setTypeCallback((datas, p) -> {
            if (datas.size() > 11) {
                arrow_layout_2.setVisibility(View.VISIBLE);
            } else {
                arrow_layout_2.setVisibility(View.GONE);
            }
            p1 = p;
            list_2.clear();
            list_3.clear();
            if (datas.size() > 0) {
                list_2.add(new CommonPopEntity("", "全部"));
                if (datas.size() > 11) {
                    list_2.addAll(datas.subList(0, 11));
                } else {
                    list_2.addAll(datas);
                }
                adapter2.setSelect(0);
                p2 = 0;
                fl_2.setVisibility(View.VISIBLE);
                l_1.setVisibility(View.VISIBLE);
            } else {
                l_1.setVisibility(View.GONE);
                l_2.setVisibility(View.GONE);
                fl_2.setVisibility(View.GONE);
                p2 = -1;
            }
            p3 = -1;
            fl_3.setVisibility(View.GONE);
            l_2.setVisibility(View.GONE);
            adapter2.notifyDataSetChanged();
            adapter3.notifyDataSetChanged();
        });
        rv_1.setAdapter(adapter1);

        rv_2.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter2 = new MemberTypeAdapter(mContext, list_2);
        adapter2.setTypeCallback((datas, p) -> {
            if (datas.size() > 11) {
                arrow_layout_3.setVisibility(View.VISIBLE);
            } else {
                arrow_layout_3.setVisibility(View.GONE);
            }
            p2 = p;
            list_3.clear();
            if (datas.size() > 0) {
                list_3.add(new CommonPopEntity("", "全部"));
                if (datas.size() > 11) {
                    list_3.addAll(datas.subList(0, 11));
                } else {
                    list_3.addAll(datas);
                }
                adapter3.setSelect(0);
                p3 = 0;
                fl_3.setVisibility(View.VISIBLE);
                l_2.setVisibility(View.VISIBLE);
            } else {
                fl_3.setVisibility(View.GONE);
                l_2.setVisibility(View.GONE);
                p3 = -1;
            }
            adapter3.notifyDataSetChanged();

        });
        rv_2.setAdapter(adapter2);

        rv_3.setLayoutManager(new GridLayoutManager(mContext, 4));
        adapter3 = new MemberTypeAdapter(mContext, list_3);
        adapter3.setTypeCallback((datas, p) -> p3 = p);
        rv_3.setAdapter(adapter3);


        view.findViewById(R.id.reset_view).setOnClickListener(v -> callback.dissDiag());
        view.findViewById(R.id.ok_view).setOnClickListener(v -> {
            StringBuilder sb = new StringBuilder();
            String no1 = "";
            if (list_1.size() > 0) {
                CommonPopEntity entity = list_1.get(p1);
                if (entity != null) {
                    no1 = entity.getNo();
                    if (!entity.getTypeName().equals("全部")) {
                        sb.append(entity.getTypeName());
                    }
                }
            }
            String no2 = "";
            if (list_2.size() > 0 && p2 != -1) {
                if (p2 != 0) {
                    CommonPopEntity entity = list_1.get(p1).getChildren().get(p2 - 1);
                    if (entity != null) {
                        no2 = entity.getNo();
                        if (!entity.getTypeName().equals("全部")) {
                            sb.append(",");
                            sb.append(entity.getTypeName());
                        }
                    }
                }
            }
            String no3 = "";
            if (list_3.size() > 0 && p3 != -1) {
                if (p2 != 0 && p3 != 0) {
                    CommonPopEntity entity = list_1.get(p1).getChildren().get(p2 - 1).getChildren().get(p3 - 1);
                    if (entity != null) {
                        no3 = entity.getNo();
                        if (!entity.getTypeName().equals("全部")) {
                            sb.append(",");
                            sb.append(entity.getTypeName());
                        }
                    }
                }
            }
            callback.back(no1, no2, no3, sb.toString());
            dismiss();
        });

        //编写数据回显
        if (list_1.size() > 0) {
            if (list_1.size() > 12) {
                arrow_layout_1.setVisibility(View.GONE);
                arrow_layout_up_1.setVisibility(View.VISIBLE);
            } else {
                arrow_layout_1.setVisibility(View.GONE);
                arrow_layout_up_1.setVisibility(View.GONE);
            }
            list_2.clear();
            for (int i = 0; i < list_1.size(); i++) {
                if (list_1.get(i).getNo().equals(firstCategory)) {
                    adapter1.setSelect(i);
                    p1 = i;
                    if (list_1.get(i).getChildren() != null) {
                        list_2.add(new CommonPopEntity("", "全部"));
                        list_2.addAll(list_1.get(i).getChildren());
                    }
                }
            }
        }
        if (list_2.size() > 0) {
            fl_2.setVisibility(View.VISIBLE);
            l_1.setVisibility(View.VISIBLE);
            if (list_2.size() > 12) {
                arrow_layout_2.setVisibility(View.GONE);
                arrow_layout_up_2.setVisibility(View.VISIBLE);
            } else {
                arrow_layout_2.setVisibility(View.GONE);
                arrow_layout_up_2.setVisibility(View.GONE);
            }
            list_3.clear();
            for (int i = 0; i < list_2.size(); i++) {
                if (list_2.get(i).getNo().equals(secondCategory)) {
                    adapter2.setSelect(i);
                    p2 = i;
                    if (list_2.get(i).getChildren() != null) {
                        list_3.add(new CommonPopEntity("", "全部"));
                        list_3.addAll(list_2.get(i).getChildren());
                    }
                }
            }
        }else {
            fl_2.setVisibility(View.GONE);
            l_1.setVisibility(View.GONE);
        }
        if (list_3.size() > 0) {
            fl_3.setVisibility(View.VISIBLE);
            l_2.setVisibility(View.VISIBLE);
            if (list_3.size() > 12) {
                arrow_layout_3.setVisibility(View.GONE);
                arrow_layout_up_3.setVisibility(View.VISIBLE);
            } else {
                arrow_layout_3.setVisibility(View.GONE);
                arrow_layout_up_3.setVisibility(View.GONE);
            }
            for (int i = 0; i < list_3.size(); i++) {
                if (list_3.get(i).getNo().equals(thirdCategory)) {
                    adapter3.setSelect(i);
                    p3 = i;
                }
            }
        }else {
            fl_3.setVisibility(View.GONE);
            l_2.setVisibility(View.GONE);
        }
        return view;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);
        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置宽
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置高
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);
        //设置dialog的动画效果
        window.setWindowAnimations(R.style.dialogWindowAnim);
        return dialog;
    }

    public interface DialogCallback {
        void back(String no1, String no2, String no3, String name);

        void dissDiag();
    }

}
